package cryptography;

/**
 *
 * @author Mouchicc & Mavs
 * @version 1.0
 */
public class Methods {

    public static int mod(int a, int b) {
        return (a % b + b) % b;
    }

    public static int charToNum(char c) {
        return ((int) Character.toLowerCase(c)) - 97;
    }

    public static String createKey(String s, String k) {
        String out = "";
        int x = 0;

        while (out.length() < s.length()) {
            if (x == k.length()) {
                x = 0;
            }
            out += k.charAt(x);
            x++;
        }
        return out;
    }

    public static int createPass(String s) {
        int out = 0;
        for (int i = 0; i < s.length(); i++) {
            out += (int) s.charAt(i);
        }
        return out;
    }

    public static int inverseMod(int p, int n, int o) {
        int out = 0;
        for (int i = 1; i < 255; i++) {
            if (((p * i) - (n * Math.floor((p * i) / n))) == o) {
                out = i;
            }
        }
        return out;
    }

    public static String stringToHex(String str) {

        char[] chars = str.toCharArray();

        StringBuffer hex = new StringBuffer();
        for (int i = 0; i < chars.length; i++) {
            hex.append(Integer.toHexString((int) chars[i]));
        }

        return hex.toString();
    }

    public static int passToInt(String s) {
        int out = 0;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            int j = (int) c;
            out += j;
        }
        return out;
    }
}
